<?php

namespace app\common\model;

use app\beautiful\model\member\MemberModel;
use app\common\model\account\ShopAccountModel;
use app\stores\model\order\GoodsOrderModel;
use think\facade\Db;


class MemberMoneyDetailModel extends CommonModel{

    protected  $name = 'member_money_detail';

    protected  $pk   = 'id';

    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }

    /**
     * @param $money 金额（元）
     * @param $status  状态：0支出，1收入
     * @param $type   类型：1下单，2买单，3,消费奖励，4股东推广提成，5股东分红奖励，6提现
     * @param $shop_id  门店id
     * @param $member_id  会员id
     * @param $partner_id  股东Id
     * @param $id  记录id
     * @param $order_type  订单类型：0下单，1买单(type=4时生效)
     * @param $money_status  到账状态：0不到账，1到账(type=4时生效)
     * @return int|string
     */
    public static function in($money,$status,$type,$shop_id,$member_id,$partner_id,$id,$order_type=0,$money_status=1){
        $MemberModel=new MemberModel();
//        if($status==0){
//            $money='-'.$money;
//        }
        $member_info=$MemberModel->where(['member_id'=>$member_id])->find();
        $insert=[];
        $insert['shop_id']=$shop_id;
        $insert['member_id']=$member_id;
        $insert['partner_id']=$partner_id;
        $insert['status']=$status;
        $insert['type']=$type;
        $insert['old_money']=$member_info['money'];
        $insert['money']=$money;
        $insert['money_status']=$money_status;
        $insert['new_money']=($member_info['money']+$money);
        if($status==0){
            $insert['money']='-'.$money;
            $insert['new_money']=($member_info['money']-$money);
        }
        $where=[];
        $where['type']=$type;
        $where['status']=$status;
        switch ($type){
            case 1:
                $insert['g_order_id']=$id;
                $where['g_order_id']=$id;
                break;
            case 2:
                $insert['b_order_id']=$id;
                $where['b_order_id']=$id;
                break;
            case 3:
                $insert['consume_bonus_id']=$id;
                $where['consume_bonus_id']=$id;
                break;
            case 4:
                if($order_type==0){
                    $insert['g_order_id']=$id;
                    $where['g_order_id']=$id;
                }else{
                    $insert['b_order_id']=$id;
                    $where['b_order_id']=$id;
                }
                break;
            case 5:
                $insert['partner_bonus_id']=$id;
                $where['partner_bonus_id']=$id;
                break;
            case 6:
                $insert['withdraw_id']=$id;
                $where['withdraw_id']=$id;
                break;
            case 7:
                $insert['g_order_id']=$id;
                $where['g_order_id']=$id;
                break;
            case 8:
                $insert['p_order_id']=$id;
                $where['p_order_id']=$id;
                break;
        }
        $insert['add_time']=date('Y-m-d H:i:s');
        if($status==0){
            $MemberModel->n_dec(['member_id'=>$member_id],'money',$money);
        }else{
            if($money_status==1){
                $MemberModel->n_inc(['member_id'=>$member_id],'money',$money);
            }
        }
        $info=self::where($where)->find();
        if(empty($info)){
            $res=self::insertGetId($insert);
        }else{
            unset($insert['add_time']);
            $insert['edit_time']=date('Y-m-d H:i:s');
            $res=self::update($insert,['id'=>$info['id']]);
        }
        return $res;
    }

    public function getList($where,$money_where,$order,$page=0,$limit=0){
        $query = $this->alias('ml')->join('shop s','ml.shop_id=s.shop_id')
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['ml.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['type']),function ($query) use ($where){
                $query->where('ml.type','=',$where['type']);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where('ml.status','=',$where['status']);
            })
            ->when(isset($where['money_status']),function ($query) use ($where){
                $query->where(['ml.money_status'=>$where['money_status']]);
            })
            ->when(!empty($where['add_start_time']),function ($query) use ($where){
                $query->where('ml.add_time','>=',$where['add_start_time']);
            })
            ->when(!empty($where['add_end_time']),function ($query) use ($where){
                $query->where('ml.add_time','<=',$where['add_end_time']);
            })->field("ml.type,ml.status,ml.money_status,ml.g_order_id,ml.money,ml.add_time,s.name as shop_name");
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();

        $out_money = $this->where($money_where)->where(['status'=>0])->sum('money');
        $out_money = sprintf("%.2f", $out_money);
        $income_money = $this->where($money_where)->where(['status'=>1])->sum('money');
        $income_money = sprintf("%.2f", $income_money);

        if(!empty($list)){
            foreach ($list as $k=>$v){
                if($v['type']==4){
                    $goods_order = GoodsOrderModel::user_order(['order_id'=>$v['g_order_id']]);
                }
                $list[$k]['tg_member_name'] = isset($goods_order)?$goods_order['nick_name']:null;
                $list[$k]['money'] = sprintf("%.2f", $v['money']);
                //是否是待解冻
                $list[$k]['is_wait_money'] = ($v['status'] == 1 && $v['money_status']==0)?1:0;
            }
        }

        return compact('count','list','out_money','income_money');
    }

    public static function list($where,$order=['a.id'=>'desc'],$page=1,$limit=10){
        $query = Db::name('member_money_detail')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('beautiful_member c','a.member_id=c.member_id')
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->field("a.member_id,a.shop_id,b.name as shop_name,c.nick_name,a.add_time");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        return compact('count','list');
    }
    public static function plist($where,$order=['a.id'=>'desc'],$page=1,$limit=10){
        $query = Db::name('member_money_detail')->alias('a')
            ->leftJoin('beautiful_order b','a.g_order_id=b.order_id and a.g_order_id>0')
            ->leftJoin('beautiful_member c','b.member_id=c.member_id and a.g_order_id>0')
            ->leftJoin('beautiful_buy_order d','a.b_order_id=d.buy_order_id and a.b_order_id>0')
            ->leftJoin('beautiful_member e','d.member_id=e.member_id and a.b_order_id>0')
            ->leftJoin('beautiful_partner_bonus f','a.partner_bonus_id=f.partner_bonus_id and a.partner_bonus_id>0')
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(isset($where['partner_id']),function ($query) use ($where){
                $query->where(['a.partner_id'=>$where['partner_id']]);
            })
            ->when(isset($where['types']),function ($query) use ($where){
                $query->where('a.type','In',$where['types']);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->field("a.id,a.add_time,a.type,c.nick_name as g_nick_name,e.nick_name as b_nick_name,a.money,f.start_time,f.end_time");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        return compact('count','list');
    }
}

